rm(list = setdiff(ls(), lsf.str()))

#UK - Understanding Society Data ---------------------
load("Study 1/Altered Data/Study1_UK_understanding.RData")
vote.UKIP<- glm(total_populist ~zero1(w3_agre) + zero1(w3_open) + zero1(w3_con)+ zero1(w3_ext) + zero1(w3_neu) +female +age + Ed_OtherQualification + Ed_GSCElevel + Ed_Alevel + Ed_higherdegree + Ed_degree + Ed_Missing + income +  zero1(w6_cynicism), data_UK, family=binomial)
UK_understanding<-data.frame(exp(cbind(coef(vote.UKIP), confint(vote.UKIP)))) 

UK_understanding<-UK_understanding[2,]                
colnames(UK_understanding )[1]="Estimate"
colnames(UK_understanding )[2]="Lo"
colnames(UK_understanding )[3]="Up"
UK_understanding$sample<-"Understanding Society"
UK_understanding$Country<-"United \n Kingdom"

#UK - British Election Studies -----------------------
load("Study 1/Altered Data/Study1_UK_BES.RData")
vote.UKIPw6 <- glm(w6_voteUKIP ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES, family="binomial")
UK_bes<-data.frame(exp(cbind(coef(vote.UKIPw6), confint(vote.UKIPw6)))) 
UK_bes_agre<-UK_bes[2,]                
colnames(UK_bes_agre )[1]="Estimate"
colnames(UK_bes_agre )[2]="Lo"
colnames(UK_bes_agre )[3]="Up"
UK_bes_agre$sample<-"UK Election 2015"
UK_bes_agre$Country<-"United \n Kingdom"

#Germany-------------------------
load("Study 1/Altered Data/study1_Germany.RData")
germany<-glm(vote_intention_populist~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age + income+income_missing + education + zero1(econ_cons)+zero1(social_cons), data=data, family="binomial",weights = z000011a)
germany_vote<-data.frame(exp(cbind(coef(germany), confint(germany)))) 
germany_vote<-germany_vote[2,]                
colnames(germany_vote )[1]="Estimate"
colnames(germany_vote )[2]="Lo"
colnames(germany_vote )[3]="Up"
germany_vote$sample<-"GESIS 2017"
germany_vote$Country<-"Germany"

#Denmark vote intention 2010-2011-----------------
load("Study 1/Altered Data/Study1_Denmark.RData")
m10<-glm(vote_populist10_intention~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+as.factor(v2010_education)+income + income_missing + zero1(econ_cons) + zero1(social) + zero1(cynicism), data=data, family="binomial")

DK_10<-data.frame(exp(cbind(coef(m10), confint(m10)))) 
DK_10<-DK_10[2,]                
colnames(DK_10 )[1]="Estimate"
colnames(DK_10 )[2]="Lo"
colnames(DK_10 )[3]="Up"
DK_10$sample<-"2010"
DK_10$Country<-"Denmark"

#Denmark vote choice in 2011
m11<-glm(vote_populist11_choice~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+as.factor(v2010_education)+income + income_missing + zero1(econ_cons) + zero1(social)  + zero1(cynicism), data=data, family="binomial")
DK_11<-data.frame(exp(cbind(coef(m11), confint(m11)))) 
DK_11<-DK_11[2,]                
colnames(DK_11)[1]="Estimate"
colnames(DK_11)[2]="Lo"
colnames(DK_11)[3]="Up"
DK_11$sample<-"2011"
DK_11$Country<-"Denmark"

#Netherlands Election 2012 -------------
load("Study 1/Altered Data/Study1_NL_12.RData")
m2<-glm(populist ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + as.factor(education) +income + income_missing + zero1(econ_cons11) + zero1(anti_immi11) + zero1(cynicism11), data=data, family="binomial")
NL_12<-data.frame(exp(cbind(coef(m2), confint(m2)))) 
NL_12<-NL_12[2,]                
colnames(NL_12 )[1]="Estimate"
colnames(NL_12 )[2]="Lo"
colnames(NL_12 )[3]="Up"
NL_12$sample<-"Election 2012"
NL_12$Country<-"Netherlands"

##Netherlands EU elections 2014 w1-------------
load("Study 1/Altered Data/Study1_NL_14.RData")
vote_intention<- glm(w1_pvv_national ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + sex + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_cyn) + zero1(w1_immi), data=data_sub, family="binomial")
EU_w1<-data.frame(exp(cbind(coef(vote_intention), confint(vote_intention)))) 
EU_w1<-EU_w1[2,]                
colnames(EU_w1 )[1]="Estimate"
colnames(EU_w1 )[2]="Lo"
colnames(EU_w1 )[3]="Up"
EU_w1$sample<-"EU 2013"
EU_w1$Country<-"Netherlands"

##Netherlands EU elections 2014 Vote Choice in EU elections-------------
vote_choiceNL<- glm(w4_pvv_EU_vote ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + sex + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_cyn) + zero1(w1_immi), data=data_sub, family="binomial")
EU_w4<-data.frame(exp(cbind(coef(vote_choiceNL), confint(vote_choiceNL)))) 
EU_w4<-EU_w4[2,]                
colnames(EU_w4 )[1]="Estimate"
colnames(EU_w4 )[2]="Lo"
colnames(EU_w4 )[3]="Up"
EU_w4$sample<-"EU 2014"
EU_w4$Country<-"Netherlands"

#Netherlands Election 2017 -------------
load("Study 1/Altered Data/Study1_NL_17.RData")
m1<-glm(populist ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age+ as.factor(education) + income + income_missing  + zero1(econ_cons) + zero1(anti_immi) + zero1(cynicism) , data=data, family="binomial")
NL_17<-data.frame(exp(cbind(coef(m1), confint(m1)))) 
NL_17<-NL_17[2,]                
colnames(NL_17 )[1]="Estimate"
colnames(NL_17 )[2]="Lo"
colnames(NL_17 )[3]="Up"
NL_17$sample<-"Election 2017"
NL_17$Country<-"Netherlands"

#Switzerland 2015 ----------------------------
load("Study 1/Altered Data/Study1_Swiss_Household15.RData")
m1<-glm(vote_svp15 ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + as.factor(language) + as.factor(education) + income + income_mis + zero1(lr_placement), data=data15, family="binomial")
swiss15<-data.frame(exp(cbind(coef(m1), confint(m1)))) 
swiss15<-swiss15[2,]                
colnames(swiss15 )[1]="Estimate"
colnames(swiss15 )[2]="Lo"
colnames(swiss15 )[3]="Up"
swiss15$sample<-"Household 2015"
swiss15$Country<-"Switzerland"

#Switzerland Elections 2015 ----------------------------
load("Study 1/Altered Data/Study1_Swiss_Elections15.RData")
swissElections15 <- glm(vote_svp ~ zero1(agre) + zero1(open) + zero1(con) + ext + neu + female + age + as.factor(W3_spr) + as.factor(education) + income + income_missing + zero1(econ_socialspending) + zero1(social_limit_immi), data=data, family="binomial")
swissElections15<-data.frame(exp(cbind(coef(swissElections15), confint(swissElections15)))) 
swissElections15<-swissElections15[2,]                
colnames(swissElections15 )[1]="Estimate"
colnames(swissElections15 )[2]="Lo"
colnames(swissElections15 )[3]="Up"
swissElections15$sample<-"Election 2015"
swissElections15$Country<-"Switzerland"

#Switzerland 2009 --------------------
load("Study 1/Altered Data/Study1_Swiss_Household09.RData")
full_att <- glm(vote_svp ~agre + open + con + ext + neu + female + age + as.factor(language) +  as.factor(education) + income + income_mis + zero1(anti_immi) + zero1(econ_cons) , data=data, family=binomial)
swiss9<-data.frame(exp(cbind(coef(full_att), confint(full_att)))) 
swiss9<-swiss9[2,]                
colnames(swiss9 )[1]="Estimate"
colnames(swiss9 )[2]="Lo"
colnames(swiss9 )[3]="Up"
swiss9$sample<-"Household 2009"
swiss9$Country<-"Switzerland"


#Spain --------------------
load("Study 1/Altered Data/Study1_Spain.Rdata")

Podemos_full <-  miceadds::glm.cluster(populist_vote ~zero1(A_critical) + zero1(open) +zero1(con)+ zero1(ext) +zero1(neu) +female +age + income + income_missing + as.factor(education) + zero1(lr_placement) + zero1(cynicism), data=data, family=binomial, cluster="CCAA")

spain<-data.frame(exp(cbind(coef(Podemos_full), confint(Podemos_full)))) 
spain<-spain[2,]                
colnames(spain )[1]="Estimate"
colnames(spain )[2]="Lo"
colnames(spain )[3]="Up"
spain$sample<-"2016"
spain$Country<-"Spain"

#Venezuela 2007 -----------------------------
load("Study 1/Altered Data/Study1_Venezuela07.RData")
mod2 <- miceadds::glm.cluster(vote_populist ~ zero1(agre) + zero1(open)+ zero1(con) +zero1(ext)+ zero1(neu)+ female+ age+ income+ income_missing+ education_years+ lr_placement + lr_placement_missing, data=data, family="binomial", cluster="venentidad")
venezuela<-data.frame(exp(cbind(coef(mod2), confint(mod2)))) 
venezuela<-venezuela[2,]                
colnames(venezuela )[1]="Estimate"
colnames(venezuela )[2]="Lo"
colnames(venezuela )[3]="Up"
venezuela$sample<-"2007"
venezuela$Country<-"Venezuela"

#US ANES 2016----------------------
load("Study 1/Altered Data/Study 1_US_ANES2016.Rdata")

Trump_vote <- glm(vote_trump ~ zero1(agreeableness) + zero1(openness) + zero1(conscientiousness) + zero1(extraversion) + zero1(neuroticism) + zero1(authoritarianism) +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + zero1(post_lib_cons_placement), data=data, family="binomial")
us_trump<-data.frame(exp(cbind(coef(Trump_vote), confint(Trump_vote)))) 
us_trump<-us_trump[2,]                
colnames(us_trump )[1]="Estimate"
colnames(us_trump )[2]="Lo"
colnames(us_trump )[3]="Up"
us_trump$sample<-"ANES 2016"
us_trump$Country<-"United\n States"


#Meta analysis---------------------
load("Study 1/Altered Data/Meta_study1.RData")
#Faster but less accurate estimate
model2 <- glmer(populist ~ agre + open + con + ext + neu +age + female + education + income + income_missing + social_cons  + econ_cons+ cynicism + lr_placement + as.factor(study) + (1|id) , data=data_meta, family="binomial", nAGQ=0)
#calculate Odds ratios
meta<-data.frame(exp(cbind(OR=fixef(model2),confint(model2,parm="beta_",method="Wald"))))
meta<-meta[2,]                
colnames(meta )[1]="Estimate"
colnames(meta )[2]="Lo"
colnames(meta )[3]="Up"
meta$sample<-"Pooled"
meta$Country<-"Pooled\n analysis"

#Create plot -------------
comb_vote<-rbind(meta, UK_understanding, UK_bes_agre, swiss9, swiss15, swissElections15, NL_12, NL_17, EU_w1, EU_w4, spain, DK_10, DK_11, venezuela, us_trump, germany_vote)
comb_vote$sample <-factor(comb_vote$sample, levels=c('Pooled', 'Understanding Society', 'Election 2015', 'UK Election 2015', 'Election 2017', 'Household 2015' , 'Household 2009', 'EU 2014', 'EU 2013', 'Election 2012','2016', '2011', '2010', '2007', 'ANES 2016', 'GESIS 2017'))
comb_vote$Country <-factor(comb_vote$Country, levels=c('United \n Kingdom', 'Germany', 'Denmark', 'Netherlands',  'Switzerland', 'Spain','Venezuela', 'United\n States', 'Pooled\n analysis'))

#Plot figure------------
fig2 <- ggplot(comb_vote ,aes(x=sample, y=Estimate, group=Country))+geom_pointrange(aes(ymin=Lo,ymax=Up), size=1)  + theme_bw()+theme(legend.position="off")+ylab("Odds ratio of voting for a populist party/politician")+xlab("")+geom_hline(yintercept=1,linetype = "dashed", color="red") + coord_flip() + facet_grid(Country~., scales = "free_y")+ theme(axis.text.x=element_text(size=14), strip.text.y=element_text(size=14)) + geom_rect(data = subset(comb_vote,Country == 'Meta \n analysis'),aes(fill = Country),xmin = -Inf,xmax = Inf, ymin = -Inf,ymax = Inf, alpha = 0.3) +  theme(strip.text.y = element_text(angle = 360))

ggsave(fig2, file="Figures/fig1_meta.pdf", dpi=900, width = 8, height =6)

